﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Data;
using System.Xml;

namespace Comm
{
    public class PublicPage : PageBase
    {
        private HtmlMeta _matekeyword = null;
        private HtmlMeta _matedescription = null;

        /// <summary>
        /// SEO 优化 MateKeyword
        /// </summary>
        protected string MetaKeywords
        {
            set { _matekeyword.Content = value; }
            get { return _matekeyword.Content; }
        }

        /// <summary>
        /// SEO 优化 MateDecription
        /// </summary>
        protected string MateDescription
        {
            set { _matedescription.Content = value; }
            get { return _matedescription.Content; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            setSEO(); 
            loadSEOXML();
            SEOSiteMap();
        }

        /// <summary>
        /// 加载SEOXML内容
        /// </summary>
        private void loadSEOXML()
        {
            string url = Request.RawUrl;
            List<MSEO> list = Cache["SEO"] as List<MSEO>;
            if (list == null)
            {
                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(Server.MapPath("/App_Data/SEOSettingsInfo.xml"));
                list = new BSEO().getList("");
                Cache.Insert("SEO",list , dep);
            }

            foreach (MSEO model in list)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(url, model.LookExpression.Trim()) && model.Enabled)
                {
                    this.Title = model.PageTitle;
                    this.MetaKeywords = model.MetaKeywords;
                    this.MateDescription = model.MetaDescription;
                    break;
                }
            }
        }

        /// <summary>
        /// 设置SEO优化
        /// </summary>
        private void setSEO()
        {
            if (Header != null)
            {
                foreach (Control c in Header.Controls)
                {
                    if (c is HtmlMeta)
                    {
                        HtmlMeta m = c as HtmlMeta;
                        if (m != null)
                        {
                            if (m.Name.ToLower() == "keywords")
                            {
                                _matekeyword = m;
                            }
                            else if (m.Name.ToLower() == "description")
                            {
                                _matedescription = m;
                            }
                        }
                    }
                }
            }

            if (Header != null)
            {
                if (_matekeyword == null)
                {
                    _matekeyword = new HtmlMeta();
                    _matekeyword.Name = "keywords";
                    _matekeyword.Content = MetaKeywords;
                    Header.Controls.Add(_matekeyword);
                }
                if (_matedescription == null)
                {
                    _matedescription = new HtmlMeta();
                    _matedescription.Name = "description";
                    _matedescription.Content = MateDescription;
                    Header.Controls.Add(_matedescription);
                }
            }
        }

        /// <summary>
        ///  处理SEO站点地图
        /// </summary>
        private void SEOSiteMap()
        {
            string cache = Request.QueryString["cache"];
            if (!string.IsNullOrEmpty(cache))
            {
                return;
            }
            string url = "http://" + Request.Url.Authority;
            if (!string.IsNullOrEmpty(Request.RawUrl))
            {
                url += Request.RawUrl;
            }
            url = System.Text.RegularExpressions.Regex.Replace(url, @"\?$|\&", "");
            if (System.Text.RegularExpressions.Regex.IsMatch(url, "/Web_Admin|/ckfinder|/ckeditor", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                return;
            }
            Comm.BSiteMap sitemapbll = new Comm.BSiteMap();
            sitemapbll.path = Server.MapPath("/sitemap.xml");
            //sitemapbll.DeleteByHistory();
            System.Collections.Generic.List<Comm.MSiteMap> list = Cache["SiteMap"] as System.Collections.Generic.List<Comm.MSiteMap>;
            if (list == null)
            {
                list = sitemapbll.getList("", "lastmod desc");
                if (list == null)
                {
                    list = new System.Collections.Generic.List<Comm.MSiteMap>();
                }
                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(Server.MapPath("/sitemap.xml"));
                Cache.Insert("SiteMap", list, dep);
            }

            Comm.MSiteMap sitemodel = list.Find(delegate(Comm.MSiteMap m) { return m.Loc == url; });
            if (sitemodel == null)
            {
                Comm.MSiteMap model = new Comm.MSiteMap();
                model.Loc = url;
                model.LastMod = DateTime.Now.ToString("yyyy-MM-dd");
                model.ChangeFreg = Comm.EChangeFreg.weekly;
                model.Priority = "0.8";
                sitemapbll.Add(model);
            }
            else
            {
                if (DateTime.Parse(sitemodel.LastMod) < DateTime.Now.AddDays(-7))
                { 
                     Comm.MSiteMap model = new Comm.MSiteMap();
                    model.Loc = url;
                    model.LastMod = DateTime.Now.ToString("yyyy-MM-dd");
                    model.ChangeFreg = Comm.EChangeFreg.weekly;
                    model.Priority = "0.8";
                    sitemapbll.Update(model);
                }
            }
        }
    }
}
